﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.ComponentModel;

namespace WeatherFrame
{
    /// <summary>
    /// A location, such as a city, whose weather is being reported.
    /// </summary>
    public class Location : INotifyPropertyChanged
    {
        internal bool IsNew { get; set; }

        public string Name { get; internal set; }
        public string Country { get; internal set; }
        public string Region { get; internal set; }
        public string Uri { get; internal set; }

        public double Latitude { get; internal set; }
        public double Longitude { get; internal set; }

        public bool IsHere { get; set; }

        public string Details
        {
            get
            {
                string s = "";
                if (!string.IsNullOrEmpty(Region)) s += Region;
                if (!string.IsNullOrEmpty(Country))
                {
                    if (s.Length > 0) s += ", ";
                    s += Country;
                }
                return s;
            }
        }

        public override string ToString()
        {
            if (IsHere) return "here";
            if (Name == null) return null;
            return Name.ToLowerInvariant();
        }

        bool isWaitingForWeather = false;
        public bool IsWaitingForWeather
        {
            get { return isWaitingForWeather; }

            set
            {
                if (IsWaitingForWeather == value) return;
                isWaitingForWeather = value;
                NotifyPropertyChanged("IsWaitingForWeather");
            }
        }

        internal List<WeatherItem> days;
        public List<WeatherItem> Days
        {
            get {
                if (days == null)
                {
                    days = new List<WeatherItem>();
                    for (int i = 0; i < 5; i++) days.Add(new WeatherItem { Location = this });
                }
                return days; 
            }
            internal set
            {
                days = value;
                if (App.ViewModel.SelectedLocation == this)
                {
                  App.ViewModel.Days = days;
                }
            }
        }

        public bool HasWeather { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        internal void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler == null) return;
            var dispatcher = System.Windows.Deployment.Current.Dispatcher;
            dispatcher.BeginInvoke(() =>
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            });
        }

        internal void SetError(string error)
        {
            WeatherItem wi = new WeatherItem { Parent = App.ViewModel, Location = this };
            wi.ErrorText = error;
            wi.IsWaitingForWeather = false;
            days.Clear();
            days.Add(wi);
            Days = days;
        }
    }
}
